<?php
/**
 * Created by PhpStorm.
 * User: lgradassi
 * Date: 02.01.2015
 * Time: 07:48
 */

class CDrawer {

    public static function Show($data, $fullcourt=0, $url="") {

        if(($url==""))
            $url = Yii::app()->request->hostInfo . Yii::app()->request->requestUri;

        // Save session variables
        $_SESSION["drawer_data"] = $data;
        $_SESSION["drawer_url"] = $url;
        $_SESSION["drawer_pixels"] = "";
        $_SESSION["drawer_height"] = "";
        $_SESSION["drawer_width"] = "";
?>


        <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" id="playbook" align="middle" height="540" width="468"><param name="allowScriptAccess" value="always">
            <param name="movie" value="graphics.swf">
            <param name="quality" value="high">
            <param name="scale" value="noscale">
            <param name="salign" value="lt">
            <param name="flashvars" value="param_id={5CA1EFD8-8B85-444D-B110-611EECCC378F}&amp;param_nr=&amp;param_mode=modify&amp;param_sessionID=896866094&amp;param_fullCourt=<?php echo $fullcourt;?>">
            <param name="swliveconnect" value="true">
            <embed src="graphics.swf" flashvars="param_id={5CA1EFD8-8B85-444D-B110-611EECCC378F}&amp;param_nr=&amp;param_mode=modify&amp;param_sessionID=896866094&amp;param_fullCourt=<?php echo $fullcourt;?>" quality="high" scale="noscale" salign="lt" name="playbook" id="playbook" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" align="middle" height="540" width="468"></embed>
        </object>


<?php
        // Show widget
        //echo "<object classid='clsid:1' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' id='playbook' align='middle' height='' width=''>";
        //echo "<param name='allowScriptAccess' value='always'>";
        //echo "<param name='movie' value='./graphics.swf'>";
        //echo "<param name='quality' value='high'>";
        //echo "<param name='scale' value='noscale'>";
        //echo "<param name='salign' value='lt'>";

        //echo "<embed src='./graphics.swf'  quality='high' scale='noscale' salign='lt' name='playbook' allowscriptaccess='always' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' align='middle' height='540' width='468'></embed></object>";
    }


    public static function Save($directory, $filename) {

        // Get session variables
        if((!isset($_SESSION["drawer_url"])) || (!isset($_SESSION["drawer_directory"])) || (!isset($_SESSION["drawer_filename"])))
            throw new CHttpException("You don't have the permission to to this (Argument missing).");

        $url = $_SESSION["drawer_url"];
        $height = $_SESSION["drawer_height"];
        $width = $_SESSION["drawer_width"];
        $pixels = $_SESSION["drawer_pixels"];

        if(($url=="") || ($height=="") || ($width=="") || ($pixels=="") || ($height=="") || ($width=="") || ($pixels==""))
            throw new CHttpException("You don't have the permission to to this (Argument missing).");


        // Generate image from $data
        $im = CDrawer::_GenerateImageFromData($width, $height, $pixels);

        // Save it into destination directory
        $ret = imagepng ($im, $directory.DIRECTORY_SEPARATOR.$filename.'.png');
        if($ret==FALSE)
            throw new CHttpException("Cannot save file to specified directory.");

        // Save the returned data
        $data = $_SESSION["drawer_data"];

        // Reset session variables
        $_SESSION["drawer_data"] = "";
        $_SESSION["drawer_url"] = "";
        $_SESSION["drawer_pixels"] = "";
        $_SESSION["drawer_height"] = "";
        $_SESSION["drawer_width"] = "";

        if($height==270)
            $type = "diag-half";
        else
            $type = "diag-full";

        return array('data'=>$data,'type'=>$type,'file_ext'=>'png');
    }



    protected static function _GenerateImageFromData($x, $y, $data) {


        $gd = imagecreatetruecolor($x, $y);

        for($j=0;$j<$y;$j++) {

            $pixels = explode(",",$data[$j]);

            for($i=0;$i<$x;$i++) {


                if(strlen($pixels[$i])==6) {
                    $rr=hexdec(substr($pixels[$i],0,2));
                    $gg=hexdec(substr($pixels[$i],2,2));
                    $bb=hexdec(substr($pixels[$i],4,2));
                }

                else if(strlen($pixels[$i])==5) {
                    $rr=hexdec(substr($pixels[$i],0,1));
                    $gg=hexdec(substr($pixels[$i],1,2));
                    $bb=hexdec(substr($pixels[$i],3,2));
                }
                else if(strlen($pixels[$i])==4) {
                    $rr=0;
                    $gg=hexdec(substr($pixels[$i],0,2));
                    $bb=hexdec(substr($pixels[$i],2,2));
                }
                else if(strlen($pixels[$i])==3) {
                    $rr=0;
                    $gg=hexdec(substr($pixels[$i],0,1));
                    $bb=hexdec(substr($pixels[$i],1,2));
                }
                else if(strlen($pixels[$i])==2) {
                    $rr=0;
                    $gg=0;
                    $bb=hexdec(substr($pixels[$i],0,2));
                }
                else if((strlen($pixels[$i])==1) && ($pixels[$i]=="g")) {
                    $rr=0xea;
                    $gg=0x86;
                    $bb=0x72;
                }
                else if(strlen($pixels[$i])==1) {
                    $rr=0;
                    $gg=0;
                    $bb=hexdec(substr($pixels[$i],0,1));
                }
                else if(strlen($pixels[$i])==0) {
                    $rr=0xf4;
                    $gg=0xcc;
                    $bb=0xc4;
                }

                $tmpColor = imagecolorallocate($gd, $rr, $gg, $bb);
                imagesetpixel($gd, $i, $j, $tmpColor);
            }
        }

        return $gd;
    }

}